package org.jeecg.modules.business.warehouse.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.BeanUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.customer.entity.BasicCustomer;
import org.jeecg.modules.basic.customer.mapper.BasicCustomerMapper;
import org.jeecg.modules.basic.material.entity.BasicMaterial;
import org.jeecg.modules.basic.material.mapper.BasicMaterialMapper;
import org.jeecg.modules.basic.supplier.entity.BasicSupplier;
import org.jeecg.modules.basic.supplier.mapper.BasicSupplierMapper;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseService;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupService;
import org.jeecg.modules.business.doc.business.entity.dto.ListWmsInReceiptDTO;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.entity.vo.*;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.InboundStatusEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: 入库任务
 * @Author: jeecg-boot
 * @Date: 2023-07-28
 * @Version: V1.0
 */
@Api(tags = "入库任务")
@RestController
@RequestMapping("/business/wmsInReceiptTask")
@Slf4j
public class WmsInReceiptTaskController {

    @Autowired
    private IWmsInReceiptTaskService wmsInReceiptTaskService;
    @Autowired
    private IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    @Autowired
    private IWmsInReceiptService wmsInReceiptService;
    @Resource
    private BasicMaterialMapper basicMaterialMapper;
    @Resource
    private BasicCustomerMapper basicCustomerMapper;
    @Resource
    private BasicSupplierMapper basicSupplierMapper;

    @Resource
    private IWmsBasicWarehouseService wmsBasicWarehouseService;
    @Autowired
    private IWmsBasicWarehouseGroupService iWmsBasicWarehouseGroupService;

    private static final String CUSTOMER = "2";

    private static final String SUPPLIER = "3";

    /**
     * 根据任务id修改任务状态 1:入库任务 2:出库任务
     *
     * @param updateStatusRequestVO
     * @return
     */
    @ApiOperation(value = "根据任务id修改任务状态 1:入库任务 2:出库任务", notes = "根据任务id修改任务状态 1:入库任务 2:出库任务")
    @PostMapping("/updateStatus")
    public Result<Void> updateStatus(@RequestBody UpdateStatusRequestVO updateStatusRequestVO) {
        wmsInReceiptTaskService.updateStatus(updateStatusRequestVO.getId(), updateStatusRequestVO.getTaskType());
        return Result.ok();
    }

    /**
     * 任务手工关闭
     *
     * @param closeTaskRequestVO
     * @return
     * @author DongPeng
     * @date 2023/9/4 9:10
     */
    @ApiOperation(value = "任务手工关闭", notes = "任务手工关闭")
    @PostMapping("/closeTask")
    public Result<Void> closeTask(@RequestBody CloseTaskRequestVO closeTaskRequestVO) {
        wmsInReceiptTaskService.closeTask(closeTaskRequestVO.getId(), closeTaskRequestVO.getTaskType());
        return Result.ok();
    }

    /**
     * 入库任务下查
     *
     * @return
     */
    @ApiOperation(value = "入库任务下查", notes = "入库任务下查")
    @GetMapping(value = "/listWmsInReceipt")
    public Result<List<ListWmsInReceiptDTO>> listWmsInReceipt(@RequestParam("wmsInReceiptId") String wmsInReceiptId) {
        List<ListWmsInReceiptDTO> list = new ArrayList<>();
        ListWmsInReceiptDTO listWmsInReceiptDTO = wmsInReceiptService.listWmsInReceipt(wmsInReceiptId);
        list.add(listWmsInReceiptDTO);
        return Result.ok(list);
    }

    /**
     * 分页列表查询
     *
     * @param wmsInReceiptTask
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "入库任务-分页列表查询")
    @ApiOperation(value = "入库任务-分页列表查询", notes = "入库任务-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "business/storage/task/WmsInReceiptTaskList")
    public Result<IPage<WmsInReceiptTaskPageVO>> queryPageList(WmsInReceiptTaskQueryVO wmsInReceiptTask,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        IPage<WmsInReceiptTask> page = new Page<>(pageNo, pageSize);
        IPage<WmsInReceiptTaskPageVO> pageList = wmsInReceiptTaskService.listByPage(page, wmsInReceiptTask);
        List<WmsInReceiptTaskPageVO> records = pageList.getRecords();
        List<String> taskIds = records.stream().map(WmsInReceiptTask::getId).collect(Collectors.toList());
        List<WmsInReceiptTaskMaterial> list = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .in(CollectionUtil.isNotEmpty(taskIds), WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskIds));
        //货主给客户赋值
        List<WmsInReceiptTask> customers = records.stream().filter(o -> CUSTOMER.equals(o.getOwnerType())).collect(Collectors.toList());
        List<String> customerIds = customers.stream().map(WmsInReceiptTask::getOwner).collect(Collectors.toList());
        List<BasicCustomer> basicCustomers = basicCustomerMapper.selectList(new LambdaQueryWrapper<BasicCustomer>()
                .in(CollectionUtil.isNotEmpty(customerIds), BasicCustomer::getId, customerIds));
        for (WmsInReceiptTask customer : customers) {
            basicCustomers.forEach(o -> {
                if (o.getId().equals(customer.getOwner())) {
                    customer.setOwner(o.getName());
                }
            });
        }
        //货主给供应商赋值
        List<WmsInReceiptTask> suppliers = records.stream().filter(o -> SUPPLIER.equals(o.getOwnerType())).collect(Collectors.toList());
        List<String> supplierIds = suppliers.stream().map(WmsInReceiptTask::getOwner).collect(Collectors.toList());
        List<BasicSupplier> basicSuppliers = basicSupplierMapper.selectList(new LambdaQueryWrapper<BasicSupplier>()
                .in(CollectionUtil.isNotEmpty(supplierIds), BasicSupplier::getId, supplierIds));
        for (WmsInReceiptTask supplier : suppliers) {
            basicSuppliers.forEach(o -> {
                if (o.getId().equals(supplier.getOwner())) {
                    supplier.setOwner(o.getName());
                }
            });
        }
        for(WmsInReceiptTaskPageVO pageVO :records){
            List<WmsInReceiptTaskMaterial> taskMaterialList = list.stream().filter(x -> x.getWmsInReceiptTaskId().equals(pageVO.getId())).collect(Collectors.toList());
            if(!CollectionUtil.isEmpty(taskMaterialList)){
                pageVO.setHasChild(1);
                Set<String> warehouseIdSet = taskMaterialList.stream().filter(o->!StrUtil.isEmpty(o.getWmsBasicWarehouseId())).map(WmsInReceiptTaskMaterial::getWmsBasicWarehouseId).collect(Collectors.toSet());
                List<WmsBasicWarehouse> warehouseList=new ArrayList<>();
                if(!CollectionUtil.isEmpty(warehouseIdSet)){
                    warehouseList = wmsBasicWarehouseService.list(new LambdaQueryWrapper<WmsBasicWarehouse>()
                            .in(CollectionUtil.isNotEmpty(warehouseIdSet), WmsBasicWarehouse::getId, warehouseIdSet)
                    );
                }
                Set<String> warehouseCodeSet = warehouseList.stream().map(WmsBasicWarehouse::getName).collect(Collectors.toSet());
                pageVO.setWarehouseCodeStr(StrUtil.join(",",warehouseCodeSet));
            }else{
                pageVO.setHasChild(0);
            }
            if (Objects.equals(TransferStatusEnum.TRANSFER_FAIL.getCode(),pageVO.getTransferSapStatus())){
                pageVO.setTransferButton(true);
            }else {
                pageVO.setTransferButton(false);
            }
        }
        pageList.setRecords(records);
        return Result.OK(pageList);
    }

    /**
     * 入库任务-获取子数据
     *
     * @param parentIds
     * @return
     */
    @ApiOperation(value = "入库任务-获取子数据", notes = "入库任务-获取子数据")
    @GetMapping("/getChild")
    public Result<List<WmsInReceiptTaskMaterial>> getChild(@RequestParam("parentIds") String parentIds) {
        List<WmsInReceiptTaskMaterial> list = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .in(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, Arrays.asList(parentIds.split(","))));

        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param wmsInReceiptTaskPage
     * @returns
     */
    @AutoLog(value = "入库任务-添加")
    @ApiOperation(value = "入库任务-添加", notes = "入库任务-添加")
    @RequiresPermissions("business:wms_in_receipt_task:add")
    @PostMapping(value = "/add")
    public Result add(@RequestBody WmsInReceiptTaskPage wmsInReceiptTaskPage) {
        Result<String> result = new Result<>();
        WmsInReceiptTask wmsInReceiptTask = new WmsInReceiptTask();
        BeanUtils.copyProperties(wmsInReceiptTaskPage, wmsInReceiptTask);
        String s = wmsInReceiptTaskService.saveMain(wmsInReceiptTask, wmsInReceiptTaskPage.getWmsInReceiptTaskMaterialList());
        result.setSuccess(true);
        result.setCode(CommonConstant.SC_OK_200);
        result.setMessage("添加成功！");
        result.setResult(s);
        return Result.OK(result);
    }

    /**
     * 编辑
     *
     * @param wmsInReceiptTaskPage
     * @return
     */
    @AutoLog(value = "入库任务-编辑")
    @ApiOperation(value = "入库任务-编辑", notes = "入库任务-编辑")
    @RequiresPermissions("business:wms_in_receipt_task:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody WmsInReceiptTaskPage wmsInReceiptTaskPage) {
        WmsInReceiptTask wmsInReceiptTask = new WmsInReceiptTask();
        BeanUtils.copyProperties(wmsInReceiptTaskPage, wmsInReceiptTask);
        WmsInReceiptTask wmsInReceiptTaskEntity = wmsInReceiptTaskService.getById(wmsInReceiptTask.getId());
        if (wmsInReceiptTaskEntity == null) {
            return Result.error("未找到对应数据");
        }
        wmsInReceiptTaskService.updateMain(wmsInReceiptTask, wmsInReceiptTaskPage.getWmsInReceiptTaskMaterialList());
//        if(wmsInReceiptTask.getDocumentStatus().equals("已提交")){
//            messageManager.sendCommand("入库任务已提交", wmsInReceiptTask.getId());
//        }
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "入库任务-通过id删除")
    @ApiOperation(value = "入库任务-通过id删除", notes = "入库任务-通过id删除")
    @RequiresPermissions("business:wms_in_receipt_task:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        wmsInReceiptTaskService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "入库任务-批量删除")
    @ApiOperation(value = "入库任务-批量删除", notes = "入库任务-批量删除")
    @RequiresPermissions("business:wms_in_receipt_task:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wmsInReceiptTaskService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "入库任务-通过id查询")
    @ApiOperation(value = "入库任务-通过id查询", notes = "入库任务-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WmsInReceiptTask> queryById(@RequestParam(name = "id", required = true) String id) {
        WmsInReceiptTask wmsInReceiptTask = wmsInReceiptTaskService.getById(id);
        if (wmsInReceiptTask == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(wmsInReceiptTask);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "入库任务-物料通过主表ID查询")
    @ApiOperation(value = "入库任务-物料主表ID查询", notes = "入库任务-物料-通主表ID查询")
    @GetMapping(value = "/queryWmsInReceiptTaskMaterialByMainId")
    public Result<List<ListByMaterialIdResponseVO>> queryWmsInReceiptTaskMaterialListByMainId(@RequestParam(name = "id", required = true) String id,
                                                                                              @RequestParam(name = "name", required = false) String name
                                                                                              ) {
        List<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialList = wmsInReceiptTaskMaterialService.selectByMainId(id);
        List<ListByMaterialIdResponseVO> list = BeanUtil.copyToList(wmsInReceiptTaskMaterialList, ListByMaterialIdResponseVO.class);
        List<String> materialIds = list.stream().map(ListByMaterialIdResponseVO::getWmsBasicMaterialId).collect(Collectors.toList());
        List<BasicMaterial> basicMaterials = basicMaterialMapper.selectList(new LambdaQueryWrapper<BasicMaterial>()
                .in(CollectionUtil.isNotEmpty(materialIds), BasicMaterial::getId, materialIds));
        for (ListByMaterialIdResponseVO vo : list) {
            basicMaterials.forEach(basicMaterial -> {
                if (basicMaterial.getId().equals(vo.getWmsBasicMaterialId())) {
                    if (ObjectUtil.isNotEmpty(basicMaterial)) {
                        vo.setMaterialCode(basicMaterial.getCode());
                        vo.setMaterialName(basicMaterial.getName());
                        vo.setSpecification(basicMaterial.getSpecification());
//                        vo.setWmsBasicWarehouseId(basicMaterial.getWarehouseId());
//                        vo.setWmsBasicWarehouseAreaId(basicMaterial.getWarehouseAreaId());
                    }
                }
            });
            //前端要求
            if (StrUtil.isNotEmpty(vo.getIsMergeRoll())){
                if ("Y".equals(vo.getIsMergeRoll())){
                    vo.setIsMergeRoll("1");
                }
                if ("N".equals(vo.getIsMergeRoll())){
                    vo.setIsMergeRoll("0");
                }
            }
        }
        if (StringUtils.isNotBlank(name)) {
            list = list.stream().filter(a -> (StringUtils.isNotBlank(a.getBatchCode()) && a.getBatchCode().contains(name)) || (StringUtils.isNotBlank(a.getMaterialCode()) && a.getMaterialCode().contains(name))).collect(Collectors.toList());
        }
        return Result.OK(list);
    }
    /**
     * 通过id查询(分页)
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "入库任务-物料通过主表ID查询")
    @ApiOperation(value = "入库任务-物料主表ID查询(分页)", notes = "入库任务-物料-通主表ID查询(分页)")
    @GetMapping(value = "/queryWmsInReceiptTaskMaterialByMainIdPage")
    public Result<IPage<ListByMaterialIdResponseVO>> queryWmsInReceiptTaskMaterialListByMainIdPage(@RequestParam(name = "id", required = true) String id,
                                                                                              @RequestParam(name = "name", required = false) String name,
                                                                                              @RequestParam(name = "deviceType", required = false) String deviceType,
                                                                                              @RequestParam(name = "unCompleted", required = false) Integer unCompleted,
                                                                                              @RequestParam(name = "wmsBasicWarehouseId", required = false) String wmsBasicWarehouseId,
                                                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        List<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialList = wmsInReceiptTaskMaterialService.selectByMainId(id);
        List<ListByMaterialIdResponseVO> list = BeanUtil.copyToList(wmsInReceiptTaskMaterialList, ListByMaterialIdResponseVO.class);
        List<String> materialIds = list.stream().map(ListByMaterialIdResponseVO::getWmsBasicMaterialId).collect(Collectors.toList());
        List<BasicMaterial> basicMaterials = basicMaterialMapper.selectList(new LambdaQueryWrapper<BasicMaterial>()
                .in(CollectionUtil.isNotEmpty(materialIds), BasicMaterial::getId, materialIds));
        for (ListByMaterialIdResponseVO vo : list) {
            basicMaterials.forEach(basicMaterial -> {
                if (basicMaterial.getId().equals(vo.getWmsBasicMaterialId())) {
                    if (ObjectUtil.isNotEmpty(basicMaterial)) {
                        vo.setMaterialCode(basicMaterial.getCode());
                        vo.setMaterialName(basicMaterial.getName());
                        vo.setSpecification(basicMaterial.getSpecification());
//                        vo.setWmsBasicWarehouseId(basicMaterial.getWarehouseId());
//                        vo.setWmsBasicWarehouseAreaId(basicMaterial.getWarehouseAreaId());
                    }
                }
            });
            //前端要求
            if (StrUtil.isNotEmpty(vo.getIsMergeRoll())){
                if ("Y".equals(vo.getIsMergeRoll())){
                    vo.setIsMergeRoll("1");
                }
                if ("N".equals(vo.getIsMergeRoll())){
                    vo.setIsMergeRoll("0");
                }
            }
        }
        if (StringUtils.isNotBlank(name)) {
            list = list.stream().filter(a -> (StringUtils.isNotBlank(a.getBatchCode()) && a.getBatchCode().contains(name)) || (StringUtils.isNotBlank(a.getMaterialCode()) && a.getMaterialCode().contains(name))).collect(Collectors.toList());
        }
        //unCompleted = 1只查询未完成的
        if (unCompleted != null && unCompleted == 1){
           list = list.stream().filter(a->!Objects.equals(a.getWarehouseStatus(), InboundStatusEnum.ALL.getCode())).collect(Collectors.toList());
        }
        //wmsBasicWarehouseId 筛选条件(pda只可查看当前仓库下的明细),仓库为空的或者为空字符串的要展示
        if (StrUtil.isNotBlank(wmsBasicWarehouseId)){
            list = list.stream().filter(a->
                            Objects.equals(a.getWmsBasicWarehouseId(), wmsBasicWarehouseId)||
                            a.getWmsBasicWarehouseId() == null||
                            StrUtil.isBlank(a.getWmsBasicWarehouseId())
            ).collect(Collectors.toList());
        }
        IPage<ListByMaterialIdResponseVO> res = new Page<>();
        List<ListByMaterialIdResponseVO> pageList = new ArrayList<>();
        try {
            pageList = ListUtil.page(pageNo-1, pageSize, list);
        }catch (Exception e){
            //前端未做页数限制,传的pageNo大于返回的pages时会报错
        }
        res.setRecords(pageList);
        res.setTotal(list.size());
        res.setPages(PageUtil.totalPage(list.size(),pageSize));
        res.setSize(pageSize);
        res.setCurrent(pageNo);
        return Result.OK(res);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wmsInReceiptTask
     */
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @RequiresPermissions("business:wms_in_receipt_task:exportXls")
    @RequestMapping(value = "/exportXls", method = RequestMethod.POST)
    public ModelAndView exportXls(HttpServletRequest request, @RequestBody WmsInReceiptTask wmsInReceiptTask) {
        // Step.1 组装查询条件查询数据
        QueryWrapper<WmsInReceiptTask> queryWrapper = QueryGenerator.initQueryWrapper(wmsInReceiptTask, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        //配置选中数据查询条件
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            queryWrapper.in("id", selectionList);
        }
        //Step.2 获取导出数据
        List<WmsInReceiptTask> wmsInReceiptTaskList = wmsInReceiptTaskService.list(queryWrapper);
        List<String> receiptIds = wmsInReceiptTaskList.stream().map(w -> w.getId()).collect(Collectors.toList());
        List<WmsInReceiptTaskMaterial> receiptTaskMaterials = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .in(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, receiptIds));
        Map<String, List<WmsInReceiptTaskMaterial>> listMap = new HashMap<>();

        if (receiptTaskMaterials != null){
            listMap = receiptTaskMaterials.stream().collect(Collectors.groupingBy(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId));
        }


        // Step.3 组装pageList
        List<WmsInReceiptTaskPage> pageList = new ArrayList<WmsInReceiptTaskPage>();
        for (WmsInReceiptTask main : wmsInReceiptTaskList) {
            WmsInReceiptTaskPage vo = new WmsInReceiptTaskPage();
            BeanUtils.copyProperties(main, vo);
            List<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialList = listMap.getOrDefault(main.getId(),new ArrayList<>());
            vo.setWmsInReceiptTaskMaterialList(wmsInReceiptTaskMaterialList);
            pageList.add(vo);
        }

        // Step.4 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "入库任务列表");
        mv.addObject(NormalExcelConstants.CLASS, WmsInReceiptTaskPage.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("入库任务数据", "导出人:" + sysUser.getRealname(), "入库任务"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    @RequiresPermissions("business:wms_in_receipt_task:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<WmsInReceiptTaskPage> list = ExcelImportUtil.importExcel(file.getInputStream(), WmsInReceiptTaskPage.class, params);
                for (WmsInReceiptTaskPage page : list) {
                    WmsInReceiptTask po = new WmsInReceiptTask();
                    BeanUtils.copyProperties(page, po);
                    wmsInReceiptTaskService.saveMain(po, page.getWmsInReceiptTaskMaterialList());
                }
                return Result.OK("文件导入成功！数据行数:" + list.size());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return Result.error("文件导入失败:" + e.getMessage());
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.OK("文件导入失败！");
    }

    /**
     * 入库-安灯接口提供调用
     *
     * @author FanZhiWen
     * @date 2023/01/17 08:44
     * @param vo 请求体
     * @return int 0:失败 1:成功
     */
    @ApiOperation(value = "入库-安灯接口提供调用", notes = "入库-安灯接口提供调用")
    @PostMapping("/call/inbound")
    public Result<Void> lightIBSHandleInbound(@RequestBody LightRequestHandleInboundVO vo) {
        return wmsInReceiptTaskService.lightIBSHandleInbound(vo);
    }
    /**
     * 入库-安灯接口批量提供调用
     *
     * @author FanZhiWen
     * @date 2023/01/17 08:44
     * @param vo 请求体
     * @return int 0:失败 1:成功
     */
    @ApiOperation(value = "入库-安灯接口批量提供调用", notes = "入库-安灯接口批量提供调用")
    @PostMapping("/call/inboundAll")
    public Result<Void> lightIBSHandleInboundAll(@RequestBody LightRequestHandleInboundAllVO vo) {
        return wmsInReceiptTaskService.lightIBSHandleInboundAll(vo);
    }
}
